package com.ys.zero.dao;

import java.util.List;

import org.junit.Test;

import com.ys.zero.bean.Collection;
import com.ys.zero.bean.Concerned;
import com.ys.zero.bean.User;
import com.ys.zero.biz.BizException;
import com.ys.zero.util.DBHelper;
import com.ys.zero.util.VerifyRound;




public class UserDao {
	/**
	 * 查询用户信息
	 * @param account
	 * @param pwd
	 * @return
	 */
         public List<User> queryuser(String account,String pwd){
        	 String sql = "select * from user where account = ? and pwd = ?";
        	 return DBHelper.query(sql, User.class, account,pwd);
        	 
         }
         
         /**
     	 * 注册插入用户信息
     	 * @throws Exception 
     	 */


     	public  String  insertRegist(Object...params) throws Exception {
     		VerifyRound v  = new VerifyRound();
     		String account = v.getRound();
     		//默认头像，vip,在线状态
     		String image = "'/img/头像1.jpg'";
     		String vip = "0";
     		String state = "1";
     		String sql = "insert into user(id,account,name,pwd,email,date,image,vip,state) VALUES(NULL,"+account+",?,?,?,now(),"
     		+image+","+vip+","+state+")";
     		int a = DBHelper.insertUser(sql,params);
     		return account;
     }
     	
     	/**
     	 * 找回密码dao
     	 * @param account
     	 * @param pwd
     	 * @return
     	 */
     	public User getBack(String account){
       	 String sql = "select * from user where account = ?";
       	 return DBHelper.query(sql, User.class, account).get(0);
        }
     	/**
     	 * 查到好友id
     	 * @param id
     	 * @return
     	 */
    	public List<Concerned> showFollow(int id){
          	 String sql = "select baccount_Id,qaccount_Id from concerned where qaccount_id = ?";
          	  return DBHelper.query(sql, Concerned.class, id); 
           }      

    	/**
     	 * 得到名片页面粉丝的id
     	 * @param id
     	 * @return
     	 */
    	public List<Concerned> getMpfs(int id){
          	 String sql = "select   baccount_Id,qaccount_Id from concerned where baccount_id = ?";
          	  return DBHelper.query(sql, Concerned.class, id); 
           }           
    	/** 
    	 * 通过得到的id获取所有信息 
    	 * @param integer
    	 * @return
    	 */
     	public List<User> getAll(Integer integer){
          	 String sql = "select * from user where id = ?";
          	 return DBHelper.query(sql, User.class, integer);
           }

     	
     	public String getEmail(String account) {
     		String sql = "select email from user where account=?";
     		return (String) DBHelper.queryValue(sql,account);
     	}
     	/**
     	 * 修改新密码
     	 * @param pwd2
     	 */
		public void insertNewPwd(String pwd2,Object account) {
			String sql = "update user set pwd = ? where account = ?";
     		try {
				DBHelper.update(sql,pwd2,account);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		@Test
		public int updateimage(String iamgePath , Object account) throws BizException {
			String sql = "update user set image = ? where account = ?";
			try {
				return DBHelper.update(sql, iamgePath , account);
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
			     throw new BizException("写入数据库失败");
				
			}
			
		}
		
		public int updateInfo(String name ,String pwd, int  sex,String introduction , String hobby, String constellation,String birthday, Object account) throws BizException {
			String sql = "update user set pwd = ? , name = ? , sex = ? , introduction = ? , hobby = ? , constellation = ? , birthday =  ? where account = ?";
			try {
				return DBHelper.update(sql, pwd,name,sex, introduction, hobby, constellation, birthday,account);
			} catch (Exception e) {
				throw new BizException("用户修改信息失败");
			}
		}
		
		
		/**
		 * 该用户的粉丝数量
		 * @param pwd2
		 * @param account
		 * @return 
		 */
		public Object getFence(int id) {
			String sql = "select  count(qaccount_id) from concerned where baccount_id = ?";
			return DBHelper.queryValue(sql, id);
		}
		/**
		 * 取消关注
		 * @param id
		 * @return
		 * @throws Exception 
		 */
		public int DeleteGz(int id,int uid) throws Exception{
         	 String sql = "DELETE from concerned where baccount_id=? and qaccount_id=? ";
         	 return DBHelper.update(sql, id,uid);
          }
		/**
		 * 关注
		 * @param id
		 * @return
		 * @throws Exception 
		 */
		public int InsertGz(int id,Integer uid) throws Exception{
         	 String sql = "INSERT into  concerned(baccount_id,qaccount_id,btime)  VALUES(?,?,NOW()) ";
         	 return DBHelper.update(sql, id,uid);
          }
		
		//查询用户收藏的文章信息  做了一个小改变  具体请看实体类
		public  List<Collection> showArticle(String account) {
			String sql = "select c.cid , c.caccount,c.ctitle, c.ctype , c.cworker , c.caddress ,u.name from collection c ,user u where caccount = ?  and c.cworker = u.id and c.cformat= 0 ";
			
			return DBHelper.query(sql, Collection.class, account);
		}
       //未完成
		public List<Collection> showVideo(String account) {
			String sql ="select c.cid , c.caccount,c.ctitle, c.ctype , c.cworker , c.caddress ,u.name from collection c ,user u where caccount = ?  and c.cworker = u.id and c.cformat= 1 ";
			
			return DBHelper.query(sql, Collection.class, account);
		}
		/**
		 * 插入收藏
		 * @param account
		 * @param uaid
		 * @param link
		 * @return
		 */
		public void insertCollection(String caccount, String uaid, String link) {
			String sql = "INSERT INTO collection VALUES(null,"
					+ " ?,?,(SELECT a.utitle FROM uarticle a where uaid = ?),"
					+ " (SELECT a.log FROM uarticle a where uaid = ?),"
					+ " (SELECT a.uid FROM uarticle a where uaid = ?)"
					+ " ,?,0"
					+ " )";
			try {
				DBHelper.update(sql,caccount,uaid,uaid,uaid,uaid,link);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		/**
		 * 删除指定收藏数据
		 * @param account
		 * @param uaid
		 */
		public void delCollection(String caccount, String link) {
			String sql = "DELETE from collection where caccount = ? and caddress=?";
			try {
				DBHelper.update(sql,caccount,link);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
}
